package dietAndroidClient;


import android.app.Activity;

import com.android.facebookcon.*;
import com.android.facebookcon.FacebookConActivity.AuthorizeListener;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;


import org.apache.http.client.ClientProtocolException;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.mnk.TCPsoketTest.R;


public class SettingActivity extends Activity implements android.view.View.OnClickListener{
	private Button mainActivityButton;
	private Button rankActivityButton;
	private Button addActivityButton;
	private Button calendarActivityButton;
	private Button facebookloginButton;
	
	private Facebook mFacebook = new Facebook(C.FACEBOOK_APP_ID);
	private Button mBtnLogin, mBtnFeed, mBtnLogout;
	private EditText mEtContent;
	
	
	  
	@Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.setting);
	    
		
	    mainActivityButton = (Button) findViewById(R.id.button1);
        mainActivityButton.setOnClickListener(this);
        
        addActivityButton = (Button) findViewById(R.id.button5);
        addActivityButton.setOnClickListener(this);

        rankActivityButton = (Button) findViewById(R.id.button3);
        rankActivityButton.setOnClickListener(this);
        
        calendarActivityButton = (Button) findViewById(R.id.button2);
        calendarActivityButton.setOnClickListener(this);

        facebookloginButton = (Button) findViewById(R.id.Button03);
        facebookloginButton.setOnClickListener(this);

   
        
//        Log.d("facebook", "11111111111111111111111111111111111");
        mEtContent = (EditText) findViewById(R.id.etContent);
//        Log.d("facebook", "222222222222222222222222");

       
        
	  }
	
	 @Override
	 public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.button1:
			Intent main = new Intent(this, DietClient.class);
			startActivity(main);
			finish();
			break;
		case R.id.button5:
			Intent add = new Intent(this, AddActivity.class);
			startActivity(add);
			finish();
			break;
		case R.id.button3:
			Intent rank = new Intent(this, RankActivity.class);
			startActivity(rank);
			finish();
			break;
		case R.id.button2:
			Intent set = new Intent(this, CalendarView.class);
			startActivity(set);
			finish();
			break;
		case R.id.Button03:
			feed();
			
			break;
	
			}
		}
	 
	 @Override
	  public void onActivityResult(int requestCode, int resultCode, Intent data)
	  {
	    super.onActivityResult(requestCode, resultCode, data);

	    if (resultCode == RESULT_OK)
	    {
	      if (requestCode == C.FACEBOOK_AUTH_CODE)
	      {
	        mFacebook.authorizeCallback(requestCode, resultCode, data);
	      }
	    }
	    else
	    {
	      if (requestCode == C.FACEBOOK_AUTH_CODE)
	      {
	        mFacebook.authorizeCallback(requestCode, resultCode, data);
	      }      
	    }
	  }
	 

	  private void login()
	  {
		
	     try
	    {
	    	 mFacebook.authorize2(this, new String[] {"publish_stream, user_photos, email"}, 
	    	    		new AuthorizeListener());
	    	 
	    	 Bundle params = new Bundle();
 		     params.putString("message", "Dieter by 8Team.");
 		     params.putString("name", "8Team");
 		     params.putString("link", "");
 		     params.putString("description", "Posted by IT Project.");
 		     params.putString("picture", "");
 		     Log.d("facebook","11111111111");
 		      mFacebook.request("me/feed", params, "POST");
 		     Log.d("facebook","2222222222222");
	   }
	    catch (Exception e)
	    {
	      e.printStackTrace();
	    }

	  }
	  
	  private void feed()
	   {
	 	  try
	 	    {
	 		  	mFacebook.authorize2(this, new String[] {"publish_stream, user_photos, email"}, 
	 	    		new AuthorizeListener());
	 		  
	 		  	Bundle params = new Bundle();
	 		     params.putString("message", "Dieter by 8Team."/*mEtContent.getText().toString()*/);
	 		     params.putString("name", "8Team");
	 		     params.putString("link", "");
	 		     params.putString("description", "Posted by IT Project.");
	 		     params.putString("picture", "");
	 		     Log.d("facebook","11111111111");
	 		      mFacebook.request("me/feed", params, "POST");
	 		     Log.d("facebook","2222222222222");
	 	    }
	 	    catch (Exception e)
	 	    {
	 	      e.printStackTrace();
	 	    }
	  }
	  
	  private void logout()
	  {
		  try
		    {
		      mFacebook.logout(this);      
		    }
		    catch (Exception e)
		    {
		      e.printStackTrace();
		    }
	  }
	
	  // Facebook  callback class
	  public class AuthorizeListener implements DialogListener
	  {
	    @Override
	    public void onCancel()
	    {
	      // TODO Auto-generated method stub
	    }

	    @Override
	    public void onComplete(Bundle values)
	    {
	      // TODO Auto-generated method stub
	      if (C.D) Log.v(C.LOG_TAG, "::: onComplete :::");
	      if (C.D) Log.v(C.LOG_TAG2, "::: onComplete :::");
	    }

	    @Override
	    public void onError(DialogError e)
	    {
	      // TODO Auto-generated method stub
	    }

	    @Override
	    public void onFacebookError(FacebookError e)
	    {
	      // TODO Auto-generated method stub
	    }
	  }

	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		
	} 
}
